<script setup>
import {useCommon} from '@/hooks/web/useCommon'
import ImportFile from "@/views/student-entry/component/ImportFile.vue";
import {usePermissions} from "@/hooks/web/usePermissions";
import {studentInsert} from "@/api/student";
import {ElMessage} from "element-plus";
const {permissions, fetchPermissions,isAdmin} = usePermissions();
const {
  studentForm,
  fetchDataBox,
  getChannel,
  channelOptions,
  schoolOptions,
  degreeOptions,
  specialtyOptions,
  getTeacher,
  teacherOptions,
  optionsBox,
  onSourceChange,
  gatherOptions,
  marketing,
  isMarketing
} = useCommon()

onMounted(() => {
  fetchDataBox('school')
  getTeacher()
  getChannel()
  gatherOptions()
  fetchPermissions()
  isMarketing()
})

const router = useRouter()

const handleBack = () => {
  router.push({path: '/student/list'})
}
let loading = ref(false)
const formRef = ref('');
let visible = ref(false)
const rules = {
  realname: [{
    required: true,
    message: '请输入学生姓名',
    trigger: 'blur'
  }],
  laiyuan: [{
    required: true,
    message: '请选择学生来源',
    trigger: 'change'
  }],
  project_id: [{
    required: true,
    message: '请选择报考项目',
    trigger: 'change'
  }],
  ruxue: [{
    required: true,
    message: '请选择计划入学时间',
    trigger: 'change'
  }],
  yixiang: [{
    required: false,
    message: '请选择意向类型',
    trigger: 'change'
  }],
  // desc: [{
  //   required: true,
  //   message: '请输入首咨信息',
  //   trigger: 'blur'
  // }],
  school_id: [{
    required: true,
    message: '请选择报考院校',
    trigger: 'change'
  }]
}

const handleSubmit = () => {
  formRef.value.validate(valid => {
    if (valid) {
      loading.value = true
      studentInsert(studentForm).then(res => {
        loading.value = false
        if (res) {
          ElMessage({message: '添加成功', type: 'success'})
          handleBack()
        }
      })
    }
  })
}

const handleDownload = ()=>{
  window.location.href = 'https://apibm.cugb-edu.cn/upload/新生注册.xlsx'
}

</script>
<template>
  <import-file :excel-visible="visible" @handle-close="visible =false"></import-file>
  <el-form style="margin-top: 30px;" label-position="left" :model="studentForm" ref="formRef" :rules="rules"
           label-width="85px" require-asterisk-position="right">
    <el-row justify="center">
      <el-col :lg="12" :xs="24" :md="24" :sm="15">
        <el-row style="justify-content: space-between;">
          <el-col :lg="24" :xs="24" :md="24" :sm="24" >
            <el-form-item>
              <el-button v-if="permissions.import" type="primary" @click="visible = true" plain icon="UploadFilled">批量导入</el-button>
              <el-button v-if="permissions.download" type="primary" @click="handleDownload" plain icon="Download">下载模板</el-button>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11">
            <el-form-item label="报考院校:" prop="school_id">
              <el-select placeholder="请选择报考院校" v-model="studentForm.school_id" @change="fetchDataBox('project')">
                <el-option v-for="item in schoolOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :md="11" :xs="24" :sm="11">
            <el-form-item label="系统时间:" prop="teacher" v-if="isAdmin">
              <el-date-picker v-model="studentForm.created_time" style="width: 100%;" type="datetime"
                              placeholder="系统时间" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"/>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11">
            <el-form-item label="报考项目:" prop="project_id">
              <el-select placeholder="请选择报考项目" v-model="studentForm.project_id" @change="fetchDataBox('zy')">
                <el-option v-for="item in degreeOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :md="11" :xs="24" :sm="11" >
            <el-form-item label="招生老师:" prop="teacher_id" v-if="!marketing">
              <el-select placeholder="请选择招生老师" v-model="studentForm.teacher_id">
                <el-option v-for="(item,index) in teacherOptions" :label="item.name" :value="item.id" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="11" :md="11" :xs="24" :sm="11">
            <el-form-item label="报考专业:" prop="zy1">
              <el-select placeholder="请选择报考专业" v-model="studentForm.zy1">
                <el-option v-for="item in specialtyOptions" :key="item.id" :label="item.name"
                           :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11">
            <el-form-item label="合作人:" prop="hezuoren">
              <el-input placeholder="请输入合作人" v-model="studentForm.hezuoren"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-divider content-position="left">学生基本信息</el-divider>
          </el-col>

          <el-col :lg="11" :xs="24" :md="11" :sm="11">
            <el-form-item label="学生姓名:" prop="realname">
              <el-input placeholder="请输入姓名" v-model="studentForm.realname"/>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :md="11" :xs="24" :sm="11">
            <el-form-item label="学生性别:" prop="sex">
              <el-select v-model="studentForm.sex">
                <el-option label="男" value="1"></el-option>
                <el-option label="女" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11">
            <el-form-item label="身份证号:" prop="idnum">
              <el-input placeholder="请输入身份证号" v-model="studentForm.idnum"/>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :md="11" :xs="24" :sm="11">
            <el-form-item label="来电号码:" prop="laidian">
              <el-input placeholder="请输入来电号码" v-model="studentForm.laidian"/>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11">
            <el-form-item label="微信名:" prop="weixin_name">
              <el-input placeholder="请输入微信名" v-model="studentForm.weixin_name"/>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :md="11" :xs="24" :sm="11">
            <el-form-item label="微信号码:" prop="weixin">
              <el-input placeholder="请输入微信号码" v-model="studentForm.weixin"/>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11">
            <el-form-item label="当前学历:" prop="xueli">
              <el-select placeholder="请选择当前学历" v-model="studentForm.xueli">
                <el-option v-for="(item,index) in optionsBox.xueli_list" :label="item.name" :value="item.value"
                           :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :md="11" :xs="24" :sm="11">
            <el-form-item label="当前专业:" prop="zhuanye">
              <el-input placeholder="请输入当前专业" v-model="studentForm.zhuanye"/>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :md="11" :xs="24" :sm="11">
            <el-form-item label="毕业学校:" prop="biye">
              <el-input placeholder="请输入毕业学校" v-model="studentForm.biye"/>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11">
            <el-form-item label="学生来源:" prop="laiyuan">
              <el-select placeholder="请选择学生来源" v-model="studentForm.laiyuan" @change="onSourceChange">
                <el-option v-for="(item,index) in optionsBox.laiyuan_list" :label="item.name" :value="item.value"
                           :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :md="11" :xs="24" :sm="11">
            <el-form-item label="来源详情:" prop="laiyuan_xiangqing">
              <el-input placeholder="请输入来源详情" v-model="studentForm.laiyuan_xiangqing"/>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11" v-show="studentForm.laiyuan === '渠道'">
            <el-form-item label="客户机构:" prop="channel_id">
              <el-select placeholder="请选择客户机构" v-model="studentForm.channel_id">
                <el-option v-for="(item,index) in channelOptions" :label="item.name" :value="item.id"
                           :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-divider content-position="left">报考信息</el-divider>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11">
            <el-form-item label="注册时间:" prop="teacher">
              <el-select placeholder="请选择注册时间" v-model="studentForm.reg_date">
                <el-option v-for="(item,index) in optionsBox.register_list" :label="item.name" :value="item.value"
                           :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :md="11" :xs="24" :sm="11">
            <el-form-item label="意向类型:" prop="yixiang">
              <el-select placeholder="请选择意向类型" v-model="studentForm.yixiang">
                <el-option v-for="(item,index) in optionsBox.yixiang_list" :label="item.name" :value="item.value"
                           :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11">
            <el-form-item label="当前状态:" prop="status">
              <el-select placeholder="请选择当前状态" v-model="studentForm.status">
                <el-option v-for="(item,index) in optionsBox.status_list" :label="item.name" :value="item.value"
                           :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="24" :md="24" :xs="24" :sm="24" v-if="!marketing">
            <el-form-item label="首咨信息:" prop="desc">
              <el-input placeholder="请输入首咨信息" :rows="5" type="textarea" v-model="studentForm.desc"/>
            </el-form-item>
          </el-col>
          <el-col :lg="11" :xs="24" :md="11" :sm="11" v-if="!marketing">
            <el-form-item label="回访时间:" prop="next_visit_time">
              <el-date-picker v-model="studentForm.next_visit_time" style="width: 100%;" type="datetime"
                              placeholder="请选择下次回访时间" format="YYYY/MM/DD HH:mm"
                              value-format="YYYY/MM/DD HH:mm"/>
            </el-form-item>
          </el-col>
          <el-col :lg="22" :xs="24" :md="24" :sm="24">
            <el-form-item prop="teacher">
              <el-button type="primary" @click="handleSubmit" :loading="loading">确认提交</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </el-form>
</template>
<style scoped lang="less">
.el-select {
  width: 100%;
  color:red !important;
}
</style>
